package models

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

func init() {
	//init
}

//	/api/v1/sbs?pid=3 查询父ID号为3的所有设备列表
func Get_sbs(pid int) (sbs []Sut_sb) {
	var (
		tmp    Sut_sb
		sqlstr string
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	sqlstr = "select sb_id , name , xinghao , guige ,changjia, remark,tag, sort from shebei where pid=?" //调用视图
	stmt, err = db.Prepare(sqlstr)                                                                       //在数据库里PID为0的同，表示设备类型
	checkErr(err)
	rows, err = stmt.Query(pid)
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&tmp.Sb_id, &tmp.Name, &tmp.Xinghao, &tmp.Guige, &tmp.Changjia, &tmp.Remark, &tmp.Tag, &tmp.Sort)
		checkErr(err)
		sbs = append(sbs, tmp)
	}
	return sbs
}

//	/api/v1/sbs?id=3	//查询ID号为3的设备
func Get_sb(sb_id int) (sb Sut_sb, err error) {
	var (
		sqlstr string
		stmt   *sql.Stmt
	)
	sqlstr = "select sb_id , name , xinghao , guige ,changjia, remark,tag, sort from shebei where sb_id=?" //调用视图
	stmt, err = db.Prepare(sqlstr)
	checkErr(err)
	err = stmt.QueryRow(sb_id).Scan(&sb.Sb_id, &sb.Name, &sb.Xinghao, &sb.Guige, &sb.Changjia, &sb.Remark, &sb.Tag, &sb.Sort)
	return sb, err
}
